package com.situ.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.situ.mall.pojo.entity.Cart;
import com.situ.mall.mapper.CartMapper;
import com.situ.mall.pojo.vo.CartVO;
import com.situ.mall.service.ICartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Gao
 * @since 2025-10-15
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements ICartService {
    @Autowired
    private CartMapper cartMapper;

    @Override
    public List<CartVO> selectByUserId(Integer userId) {
        return cartMapper.selectByUserId(userId);
    }

    @Override
    public void add(Cart cart) {
        QueryWrapper<Cart> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", cart.getUserId());
        wrapper.eq("product_id", cart.getProductId());
        Cart c = cartMapper.selectOne(wrapper);
        if (c != null) {
            c.setCount(c.getCount() + cart.getCount());
            cartMapper.updateById(c);
        } else {
            cartMapper.insert(cart);
        }
    }

    @Override
    public List<CartVO> selectByIds(Integer[] cartIds) {
        return cartMapper.selectByIds(cartIds);
    }
}
